/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/Lobby.fx $
******************************************************************************/
package de.hft_stuttgart.robofight.client.javafx;

/*
 * Lobby.fx
 *
 * @author ralf.dauenhauer@gmx.de, michael.rieker@web.de
 * Created on 03.05.2009, 12:51:10
 */
import de.hft_stuttgart.robofight.client.java.JLobby;
import de.hft_stuttgart.robofight.client.java.JPlayer;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import de.hft_stuttgart.botwar.common.commands.ChatCmd;
import javafx.scene.control.TextBox;
import com.sun.javafx.scene.control.caspian.TextBoxSkin;
import com.sun.javafx.scene.control.caspian.ButtonSkin;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import de.hft_stuttgart.botwar.common.interfaces.IRemotePlayer;

import javafx.scene.text.Text;


import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;

import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;


/*
*   Lobby Class, createGames, joinGames, showGames, Lobbychat ..
*/
public class Lobby extends CustomNode {

    package var getPlayer : function() : JPlayer;
    package var getFxzNodes : function() : FxzLoader;
    package var getNodeByName : function(nodeName : String) : Node;
    package var switchNode : function(className : String) : Void;
    public-init var jLobby = new JLobby(getPlayer());
    public var gameList : java.util.List;
    public var playerList : java.util.List;
    var showTooltip : Boolean = false;
    var tooltipText : String;

    public var chatList : java.util.List;
    var chatTextBox : TextBox =  TextBox {
        font : Constants.FONT_TEXTFIELD,
        layoutX : 162.0, layoutY : 520.0
        width: 400
        height : 18
        skin: Constants.getTextBoxSkin();
        onKeyPressed: function(evt: KeyEvent): Void {
             if (evt.code == KeyCode.VK_ENTER) {
                sendChatMsg();
             }
        }
    }

    // check chat messages
    var checkChatMsg : Timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames :
            KeyFrame {
                time : 1s
                canSkip : true
                action: function() {
                    refreshChat();
                }
            }
    }

    // check actual infos about players and games
    var checkInfos : Timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames : 
            KeyFrame {
                time : 3s
                canSkip : true
                action: function() {
                    createGameList();
                    createPlayerList();
                }
            }
    }


    var sendButton = javafx.scene.control.Button {
        cursor: Cursor.HAND
        text: "send"
        layoutY : 518
        layoutX : 580
        height: 22
        font : Constants.FONT_TEXTFIELD,
        action: function() {
           sendChatMsg();
        }
        skin: ButtonSkin {
            borderFill: Color.BLACK
            cornerRadius: 15
            highlightFill: Color.BLACK
            shadowFill: Color.BLACK
            textFill: Color.BLACK
            fill: Color.GRAY
        }
    }

     public var chatTable = TableNode {
        layoutX : 160, layoutY : 429
        scrollStepSize: -1
        w: 500
        h: 80
    }

    function refreshChat(){
        chatList = getPlayer().getLobbyChatMsg();
        var cnt = 0;
        var yPos : Number = 12;
        var tmpCnt : Integer = chatTable.tableEntries.size();
        var ct : Boolean = false;
        if(chatList.size() > tmpCnt){
        chatTable.tableEntries = null;
        while (cnt < chatList.size() or ct){
            var message : ChatCmd;
            var color = Color.WHITE;
            message = chatList.get(cnt) as ChatCmd;
            if(message.getChatType().equals(IRemotePlayer.CHATTYPE.PRIVATE)){
                color = Color.rgb(23, 185, 241);
            }
            insert Text {
                content: "{message.getChatSender() as String}: {message.getChatMessage() as String}";
                font: Constants.FONT_CHAT
                fill: color
                layoutY: yPos
            } into chatTable.tableEntries;
            yPos += 12;
            cnt++;
            }
            if(chatTable.tableEntries.size()>6){
                chatTable.scrollUp();
            }
        }
    }


   /**
    * creates and refreshes the PlayerList, only if something has changed
    */
    function createPlayerList(){
        if (playerList != getPlayer().getListOfPlayers()){
            playerList = getPlayer().getListOfPlayers();
            var cnt : Integer = 0;
            var entry : TableEntry;
            var yPos :Number = 20;
            playerTable.tableEntries = null;
            while(cnt < playerList.size()){
                 entry = TableEntry {
                                var tmp = cnt;
                                textColumns : 300
                                text: getPlayer().getShortPlayerInfo(playerList.get(cnt).toString());
                                infoBox :  getNodeByName("infoEntry") as Group;
                                switching : false
                                tooltipAction : function() : String {
                                    getPlayer().getFullPlayerInfo(playerList.get(tmp).toString());
                                }
                                y: yPos
                 }
                 yPos += entry.boundsInLocal.height;
                 insert entry into playerTable.tableEntries;
                 cnt++;
            }
         }
    }

   /**
    * creates and refreshes the GaemList, only if something has changed
    */
    function createGameList(){
        if (gameList != getPlayer().getListOfGames()){
            gameList = getPlayer().getListOfGames();
            var cnt : Integer = 0;
            var entry : TableEntry;
            var yPos : Number = 20;
            gameTable.tableEntries = null;
            while(cnt < gameList.size()){
                  var gameStatus : Node;
                  if(getPlayer().getGameStatus(gameList.get(cnt).toString())){
                      gameStatus = getNodeByName("redLight")
                  } else {
                      gameStatus = getNodeByName("greenLight")
                  }
                  entry = TableEntry {
                            text: getPlayer().getShortGameInfo(gameList.get(cnt).toString());
                            infoBox :  getNodeByName("infoEntry") as Group;
                            textColumns : 300
                            var tmp = cnt;
                            switching : false
                            statusLight : gameStatus
                            entryAction: function() {
                                if(not jLobby.enterGame(gameList.get(tmp).toString())){
                                    insert Message {
                                            msg: "Sorry, this game is already full!"
                                            msgBoxBackground: getNodeByName("msgBoxmessageBox")

                                            } into group.content;
                                } else {
                                    switchNode("GameLobby");
                                    stopAllTimelines();
                                }
                            }
                            tooltipAction : function() : String {
                                getPlayer().getFullGameInfo(gameList.get(tmp).toString());
                            }
                            y: yPos
                    }
                    yPos += entry.boundsInLocal.height;
                    insert entry into gameTable.tableEntries;
                    cnt++;;
                    }
         } 
    }

    var gameTable = TableNode {
            layoutX : 435, layoutY : 130
            w : 350
            h : 265;
    }

    var playerTable = TableNode {
            layoutX : 45, layoutY : 130
            w : 350
            h : 265;
    }

    var group : Group = Group {
            content: [
                getNodeByName("background"),
                getNodeByName("logo_small"),
                Rectangle {
                    var fxzLayout = getNodeByName("logout").layoutBounds;
                    x : fxzLayout.minX;
                    y : fxzLayout.minY;
                    width : 80
                    height : 15
                    cursor : Cursor.HAND;
                    onMouseClicked : function( e : MouseEvent) : Void {
                        getPlayer().logout();
                        switchNode("Login");
                    }
                    opacity:0
                },
                getNodeByName("lobbyCurrentGamesFrame"),
                getNodeByName("lobbyCurrentGames"),
                getNodeByName("lobbyPlayerOnlineFrame"),
                getNodeByName("lobbyPlayerOnline"),
                getNodeByName("lobbyChat"),
                getNodeByName("logout"),
                Button {
                    layoutX : getNodeByName("lobbyButtonShop").layoutBounds.minX
                    layoutY : getNodeByName("lobbyButtonShop").layoutBounds.minY
                    text: "botShop"
                    action: function():Void {
                        stopAllTimelines();
                        switchNode("BotShop");
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                },
                Button {
                    layoutX : getNodeByName("lobbyButtonCreateGame").layoutBounds.minX
                    layoutY : getNodeByName("lobbyButtonCreateGame").layoutBounds.minY
                    text: "createGame"
                    action: function():Void {
                        stopAllTimelines();
                        createGame();
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                },
                Button {
                    layoutX : getNodeByName("lobbyButtonHighScore").layoutBounds.minX
                    layoutY : getNodeByName("lobbyButtonHighScore").layoutBounds.minY
                    text: "highScore"
                    action: function():Void {
                        stopAllTimelines();
                        switchNode("HighScore");
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                },
                gameTable,
                playerTable,
                chatTextBox,
                sendButton,
                chatTable,
            ]
     };

    init {
        chatTextBox.requestFocus();
        createGameList();
        createPlayerList();
        checkInfos.play();
        checkChatMsg.play();
    }

    public function stopAllTimelines() : Void {
        checkInfos.stop();
        checkChatMsg.stop();
        getPlayer().cleanChatMessages();
    }

    // returns Lobby Node
    public override function create(): Node {
        return group;
    }

    function createGame(){
       stopAllTimelines();
       switchNode("CreateGame");
    }

    function sendChatMsg(): Void{
        if(chatTextBox.text.length() < 71){
            var tmp : String = getPlayer().sendLobbyChatMsg({chatTextBox.text} as String);
            if(tmp==""){
                chatTextBox.text = "";
                chatTextBox.requestFocus();
            } else {
                insert Message {
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")
                    title: "Info"
                    msg: tmp
                } into group.content;
            }
        } else {
            insert Message {
                msgBoxBackground: getNodeByName("msgBoxmessageBox")
                title: "Info"
                msg: "Message contains to much characters. Not more than 70 allowed!"
            } into group.content;
        }


    }


}
